package com.cell.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cell.common.States;
import com.cell.common.exceptions.exceptions.PointAccountException;
import com.cell.common.exceptions.exceptions.PointRewardException;
import com.cell.dao.user.pointaccount.UserPointOperateDao;
import com.cell.pojo.entity.system.PointReward;
import com.cell.pojo.entity.user.pointaccount.UserPointAccount;
import com.cell.dao.user.pointaccount.UserPointAccountDao;
import com.cell.pojo.entity.user.pointaccount.UserPointOperate;
import com.cell.service.system.IPointRewardService;
import com.cell.service.user.IUserPointAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@Service
public class UserPointAccountServiceImpl extends ServiceImpl<UserPointAccountDao, UserPointAccount> implements IUserPointAccountService {
    @Autowired
    UserPointAccountDao pointAccountDao;
    @Autowired
    UserPointOperateDao pointOperateDao;
    @Autowired
    IPointRewardService pointRewardService;


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void redeemProduct(Long pointAccountId, Long productId) {
        UserPointAccount pointAccount = pointAccountDao.selectById(productId);
        PointReward pointReward = pointRewardService.getById(productId);

        //无法兑换的情况
        if (pointReward.getInventory() <= 0) {
            throw new PointAccountException("库存不足", 52000);
        } else if (pointReward.getPointsRequired() > pointAccount.getBalance()) {
            throw new PointAccountException("积分不足", 52000);
        }

        //兑换商品
        pointReward.setInventory(pointReward.getInventory() - 1);
        pointAccount.setBalance(pointAccount.getBalance() - pointReward.getPointsRequired());
        //判断兑换是否成功
        int i = pointAccountDao.updateById(pointAccount);
        boolean b = pointRewardService.updateById(pointReward);
        if (i == 0 || !b) {
            throw new PointRewardException(States.ERR_POINT_UPDATE.getStates(), States.ERR_POINT_UPDATE.getDescription());
        }

        //生成记录并记录入数据库
        UserPointOperate pointOperate = new UserPointOperate();
        pointOperate.setOperate("take " + pointReward.getPointsRequired());
        pointOperate.setAccountId(pointAccountId);
        pointOperate.setTime(LocalDateTime.now());
        pointOperate.setOperatedBalance(pointAccount.getBalance());
        pointOperateDao.insert(pointOperate);
    }

    @Override
    public List<Object> getOperateHistory(Long accountId, String operate) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.likeRight(operate != null, "operate", operate);
        wrapper.eq("account_id", accountId);


        return pointOperateDao.selectList(wrapper);
    }

    @Override
    public void deletePointOperate(Long operateId) {
        int i = pointOperateDao.deleteById(operateId);
        if (i == 0) {
            throw new PointAccountException(States.ERR_USER_NOT_FOUND.getDescription(), States.ERR_USER_NOT_FOUND.getStates());
        }
    }
}
